<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use Medz\IdentityCard\China\Identity;
use App\Models\Shop;
use Validator;
use Auth;

class ShopController extends ApiController
{

    /**
     * 店铺详情
     *
     * @return \Illuminate\Http\Response
     */
    public function details()
    {
        return $this->success(Auth::guard('shop')->user());
    }

    /**
     * 更新店铺信息.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $messages = [
            'name.required' => '请填写店铺名称',
            'avatar.required' => '请上传店铺LOGO',
            'profile.required' => '请填写店铺简介',
            'address.required' => '请填写店铺地址',
            'tel.required' => '请填写电话号码'
        ];

        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'avatar' => 'required',
            'profile' => 'required',
            'address' => 'required',
            'tel' => 'required',
        ], $messages);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $shop = Shop::owner()->where('id', $id);

        if ($shop) {
            return $this->failed('');
        }

        $shop->name = $request->input('name');
        $shop->avatar = $request->input('avatar');
        $shop->profile = $request->input('profile');
        $shop->address = $request->input('address');
        $shop->tel = $request->input('tel');
    
        $shop->save();

        return $this->message('修改成功');
    }

    /**
     * Undocumented function
     *
     * @return void
     */
    public function realNameInfo()
    {
        $shop = Shop::find(Auth::guard('shop')->user()->id);
        $user = $shop->user;

        $resData = [
            'id' => $user->id,
            'realname' => $user->realname, // 真实姓名
            'idcard' => $user->idcard, // 身份证号码
            'idcard_pic' => $user->idcard_pic, // 身份证图片
        ];

        return $this->success($resData);
    }

    /**
     * 验证
     *
     * @return void
     */
    public function realNameVerification()
    {

    }
}
